package com.dale.controller;


import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dale.common.lang.Result;
import com.dale.entity.Blog;
import com.dale.entity.CollectBlog;
import com.dale.entity.LikeBLog;
import com.dale.service.BlogService;
import com.dale.service.LikeBlogService;
import com.dale.util.ShiroUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author ：MarkerHub
 * @since 2020-05-25
 */

@Slf4j
@RestController
public class LikeBlogController {

    @Autowired
    private LikeBlogService likeBlogService;

    @Autowired
    private BlogService blogService;

    @Value("${server.port}")
    private String port;


    @RequiresAuthentication
    @GetMapping("/likeBlog/{bid}")
    @RequiresRoles(value = {"common", "admin"}, logical = Logical.OR)     //表示是普通用户 或者 admin用户可访问
    public Result likeBlog(@PathVariable Long bid) {
        Long uid = ShiroUtil.getProfile().getId();

        //如果点过赞了则拒绝点
        LikeBLog one = likeBlogService.getOne(new QueryWrapper<LikeBLog>()
                .eq("bid", bid)
                .eq("uid", uid)
        );


        //拒绝点赞
        Assert.isTrue(one == null, "你已经点过赞了");


        //先维护表
//        likeBlogService.likeBlog(bid,uid);
        //感觉没必要自己写业务...

        LikeBLog likeBLog = new LikeBLog();
        likeBLog.setUid(uid);
        likeBLog.setBid(bid);
        likeBLog.setCreated(LocalDateTime.now());

        likeBlogService.save(likeBLog);


        //还要让blog的like+1 就在这改了
        Blog blog = blogService.getById(bid);
        blog.setLikeCount(blog.getLikeCount() + 1);
        blogService.updateById(blog);

        //返回likeBlog判断用户是否点过赞
        /*
        没这么简单
        created的时候要查询一下
         */
        return Result.succ(likeBLog);
    }

    @RequiresAuthentication
    @GetMapping("/dislikeBlog/{bid}")
    @RequiresRoles(value = {"common", "admin"}, logical = Logical.OR)     //表示是普通用户 或者 admin用户可访问
    public Result dislikeBlog(@PathVariable Long bid) {
        LikeBLog one = likeBlogService.getOne(new QueryWrapper<LikeBLog>().eq("uid", ShiroUtil.getProfile().getId()).eq("bid", bid));

        Assert.isTrue(one != null, "取消点赞错误");
        //不等于空 表示点赞存在 取消点赞

        likeBlogService.removeById(one.getLbid());

        return Result.succ(null);
    }


    /**
     * 返回是否点过赞信息
     *
     * @param bid
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/isLike/{bid}")
    @RequiresRoles(value = {"common", "admin"}, logical = Logical.OR)     //表示是普通用户 或者 admin用户可访问
    public Result isLike(@PathVariable Long bid) {
        Long uid = ShiroUtil.getProfile().getId();

        //如果点过赞了则拒绝点
        LikeBLog one = likeBlogService.getOne(new QueryWrapper<LikeBLog>()
                .eq("bid", bid)
                .eq("uid", uid)
        );

        if (one != null) {
            //点过赞
            return Result.succ(MapUtil.builder()
                    .put("isLike", true)
                    .map());
        } else {
            return Result.succ(MapUtil.builder()
                    .put("isLike", false)
                    .map());
        }
    }


    /**
     * 展示点赞文章
     */
    @RequiresAuthentication
    @PostMapping("/showMyLike")
    @RequiresRoles(value = {"common", "admin"}, logical = Logical.OR)
    public Result showMyLike() {

        //根据用户id 查找用户收藏的博客
        List<LikeBLog> likeBlogs = likeBlogService.list(new QueryWrapper<LikeBLog>()
                .eq("uid", ShiroUtil.getProfile().getId())
                .orderByDesc("created")
        );

        ArrayList<Blog> blogs = new ArrayList<>();
        for (LikeBLog likeBLog : likeBlogs) {
            Blog byId = blogService.getOne(new QueryWrapper<Blog>().eq("id", likeBLog.getBid()));
            if (byId != null)
                blogs.add(byId);
        }

        //将用户收藏的文章返回
        return Result.succ(blogs);
    }

}
